package com.cdtu.controller;

import com.cdtu.entity.SysEmployee;
import com.cdtu.entity.SysExam;
import com.cdtu.entity.SysRecruit;
import com.cdtu.service.SysEmployeeService;
import com.cdtu.service.SysRecruitService;
import com.cdtu.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@RequestMapping("/recruit")
//解决跨域问题
@CrossOrigin
public class SysRecruitController extends BaseController{
    @Autowired
    private SysRecruitService recruitService;
    @Autowired
    private SysEmployeeService sysEmployeeService;

    //列表
    @RequestMapping("/list")
    public ModelAndView recruitList(ModelAndView modelAndView) {

        List<SysRecruit> list = recruitService.list();
        List<SysEmployee> list1 = sysEmployeeService.list();

        //返回数据
        modelAndView.addObject("sysrecruitList", list);
        modelAndView.addObject("sysemployeeList", list1);
        //2.跳转页面
        modelAndView.setViewName("system/sysRecruit/recruitList");
        return modelAndView;
    }
    //删除
    @RequestMapping("/delete/{id}")
    public ModelAndView delete(@PathVariable("id") long id,ModelAndView modelAndView){
        boolean remove = recruitService.removeById(id);
        modelAndView.setViewName("redirect:/recruit/list");
        return modelAndView;
    }
    //添加UI
    @RequestMapping("/toAddUI")
    public ModelAndView toAddSysRecruit(ModelAndView modelAndView) {
        //2.跳转页面;
        modelAndView.setViewName("system/sysRecruit/addSysRecruit");
        return modelAndView;
    }
    //添加
    @RequestMapping("/addRecruit")
    public ModelAndView addExamUI(SysRecruit sysRecruit, ModelAndView modelAndView) {
//        System.out.println(sysRecruit);
        recruitService.save(sysRecruit);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/recruit/list");
        return modelAndView;
    }

    //修改UI
    @RequestMapping("/toUpdateUI/{id}")
    public ModelAndView toUpdateUi(ModelAndView modelAndView, @PathVariable("id") Long id) {

        SysRecruit sysRecruit = recruitService.getById(id);
        System.out.println(sysRecruit);

        //返回数据
        modelAndView.addObject("sysRecruit", sysRecruit);

        //2.跳转页面;
        modelAndView.setViewName("system/sysRecruit/updateSysRecruit");
        return modelAndView;
    }
    //修改
    @RequestMapping("/update")
    public ModelAndView update(ModelAndView modelAndView, SysRecruit sysRecruit) {

        recruitService.updateById(sysRecruit);
        //2.跳转页面;
        modelAndView.setViewName("redirect:/recruit/list");
        return modelAndView;
    }

}
